package com.example.myapplication.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class BarView extends RelativeLayout
        implements View.OnClickListener {

    private List<BtnItemView> btnList;
    private ViewPager mViewPager;

    private BtnItemView animBtn;
    private BtnItemView wordsBtn;

    public BarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.bar_layout, this);

        animBtn = findViewById(R.id.anim);
        wordsBtn = findViewById(R.id.words);

        animBtn.setText("动画");
        wordsBtn.setText("单词");

        animBtn.setOnClickListener(this);
        wordsBtn.setOnClickListener(this);

        btnList = new ArrayList<>();
        btnList.add(animBtn);
        btnList.add(wordsBtn);

        animBtn.setSelected(true);
        wordsBtn.setSelected(false);

    }
    //TODO 当Fragment切换时 设置btn的颜色
    @Override
    public void onClick(View view) {
        animBtn.setSelected(false);
        wordsBtn.setSelected(false);

        switch (view.getId()) {
            case R.id.anim:
                //切换动画Fragment
                mViewPager.setCurrentItem(0);
                animBtn.setSelected(true);
                break;
            case R.id.words:
                //切换单词Fragment
                mViewPager.setCurrentItem(1);
                wordsBtn.setSelected(true);
                break;
        }
    }

    public void setViewPager(ViewPager vp) {
        mViewPager = vp;

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                animBtn.setSelected(position == 0);
                wordsBtn.setSelected(position == 1);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
